#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# ======================================================
# @File:      : utils
# @Author     : forward_huan
# @Date       : 2025/5/28 20:28
# @Desc       :
# ======================================================
from common.utils.config_utils import getConfigData, saveConfigData
from common.utils.utils import getAllIp
from upload_file.utils.path_utils import get_params_path


def get_flask_conf():
    conf: dict = getConfigData("flask", file_path=get_params_path(), default={})
    conf.setdefault("DEBUG", False)
    conf.setdefault("SQLALCHEMY_ECHO", True)
    conf.setdefault("JSON_AS_ASCII", False)
    conf.setdefault("SQLALCHEMY_TRACE_MODIFICATIONS", False)
    saveConfigData("flask", conf, file_path=get_params_path())
    conf["ENV"] = "development" if conf.get("DEBUG", False) else "production"
    return conf


def get_address_conf():
    conf: dict = getConfigData("ip_address", file_path=get_params_path(), default={})
    conf.setdefault("host", "0.0.0.0")
    conf.setdefault("port", 5000)
    saveConfigData("ip_address", conf, file_path=get_params_path())
    return conf


def get_url_address():
    conf: dict = getConfigData("url_address", file_path=get_params_path(), default={})
    conf.setdefault("host", ",".join(getAllIp(contain_127=False)))
    conf.setdefault("port", get_address_conf()["port"])
    saveConfigData("url_address", conf, file_path=get_params_path())
    return "http://{}:{}".format(conf["host"], conf["port"])
